package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.DiagnosisService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.masterData.DiagnosisDelDto;
import com.ccys.db.dto.masterData.DiagnosisPageQueryDto;
import com.ccys.db.dto.masterData.DiagnosisSaveDto;
import com.ccys.db.entity.TblDiagnosis;
import com.ccys.db.mapper.TblDiagnosisMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.masterData.DiagnosisListVo;
import com.ccys.db.vo.masterData.DiagnosisPageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 诊断实现类
 * @author: ZhangJingCan
 * @create: 2023-12-12 16:53
 **/

@Service
public class DiagnosisServiceImpl extends BaseServiceImpl<TblDiagnosis> implements DiagnosisService {

    @Resource
    private TblDiagnosisMapper tblDiagnosisMapper;
    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult save(DiagnosisSaveDto dto) {
        //校验 - 集团ID存在并且code存在 - 需要检查是否存在相同并且未删除的数据
        if (ObjectUtil.isNotEmpty(dto.getGroupId()) && ObjectUtil.isNotEmpty(dto.getCode())) {
            //准备查询条件
            Example example = new Example(TblDiagnosis.class);
            example.createCriteria()
                    .andEqualTo("groupId", dto.getGroupId())
                    .andEqualTo("code", dto.getCode())
                    .andEqualTo("delFlag", 0);
            //检查ID有值则属于修改 - 需要排除自身
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                example.createCriteria().andNotEqualTo("id", dto.getId());
            }
            //查询并且判断 - 存在则返回错误信息
            if (ObjectUtil.isNotEmpty(tblDiagnosisMapper.selectOneByExample(example))) {
                return ResponseResult.error("A code with the same name already exists in the current Group!");
            }
        }
        if (ObjectUtil.isNotEmpty(dto.getId()) && ObjectUtil.isNotEmpty(dto.getStatus())) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
        }
        //开始新增或修改 - 并直接返回是否成功
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult delete(DiagnosisDelDto dto) {
        //判断是否传入ID - 未传入抛出错误信息
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据 - 软删除
        TblDiagnosis diagnosis = new TblDiagnosis();
        diagnosis.setId(dto.getId());
        diagnosis.setDelFlag(1);
        //操作数据库并返回
        if (tblDiagnosisMapper.updateByPrimaryKeySelective(diagnosis) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult findPage(DiagnosisPageQueryDto dto) {
        //校验值是否在范围 - 不在则赋值为null
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        PageInfo<DiagnosisPageQueryVo> info = new PageInfo<>(tblDiagnosisMapper.findPage(dto));
        return ResponseResult.success(info);
    }

    @Override
    public ResponseResult<List<DiagnosisListVo>, Object> findList() {
        List<DiagnosisListVo> list = tblDiagnosisMapper.findList();
        return ResponseResult.success(list);
    }
}
